package com.pricl.frame.util;

import java.util.Iterator;
import java.util.Set;

import com.pricl.frame.cache.MyRedisConfig;

import redis.clients.jedis.Jedis;

public class RedisUtil {
	
	/**
	 * put
	 * @param key
	 * @param value
	 * @param mill
	 */
	public static void put(String key, String value, Integer mill) {
		try (Jedis jedis = getJedis()) {
			jedis.set(key,value);
			if (mill != null) {
				jedis.expire(key, mill);
			}
		}
	}
	
	/**
	 * 获取value
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		try (Jedis jedis = getJedis()) {
			return jedis.get(key);
		}
	}
	
	/**
	 * 批量删除
	 * @param str
	 */
	public static void batchDel(String str) {
		try (Jedis jedis = getJedis()) {
			Set<String> set = jedis.keys(str);
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String keyStr = it.next();
				System.out.println(keyStr);
				jedis.del(keyStr);
			}
		}
	}
	
	static Jedis getJedis() {
		Jedis jedis = new Jedis(MyRedisConfig.getInstance().getHost(), Integer.valueOf(MyRedisConfig.getInstance().getPort()));
		jedis.auth(MyRedisConfig.getInstance().getPassword());
		jedis.select(Integer.valueOf(MyRedisConfig.getInstance().getDatabase()));
		return jedis;
	}
	
}
